use crate::riscv::*;

impl RiscV {
    #[allow(unused)]
    pub fn eliminate_meaningless_move(&mut self) {
        for func in self.text.funcs.iter_mut() {
            func.eliminate_meaningless_move();
        }
    }
}

impl AsmFunc {
    pub fn eliminate_meaningless_move(&mut self) {
        for block in self.blocks.iter_mut() {
            let mut eliminated_idx = Vec::new();
            for (idx, instr) in block.instrs.iter_mut().enumerate() {
                instr.rewrite(
                    |ty| {
                        ty == AsmInstrType::Mv || ty == AsmInstrType::Fmv
                    },
                    |ouput, inputs| {
                        if let Some(output) = ouput {
                            if inputs.into_iter().all(|input| output == input) {
                                eliminated_idx.push(idx);
                            }
                        } // if let Some
                    });
            }
            for idx in eliminated_idx.into_iter().rev() {
                block.instrs.remove(idx);
            }
        } // for block
    } // fn
} // impl

